import { defineStore } from 'pinia'
import type { TeacherPairApplication } from '../types/TeacherPairApplication'

/**
 * 教师结对申请存储模块
 * 负责管理教师结对申请的全局状态
 */
export const useTeacherPairApplicationStore = defineStore('teacherPairApplication', {
    state: () => ({
        // 所有申请列表
        applications: [] as TeacherPairApplication[],
        // 当前选中的申请
        currentApplication: null as TeacherPairApplication | null,
        // 加载状态
        loading: false,
        // 筛选条件
        filter: {
            pairType: null as number | null,
            status: null as number | null
        }
    }),
    actions: {
        /**
         * 获取所有申请
         */
        async fetchAllApplications() {
            this.loading = true
            try {
                // this.applications = await applicationApi.getAllApplications()
            } finally {
                this.loading = false
            }
        },

        /**
         * 提交新申请
         * @param newApplication - 新申请对象
         */
        async submitApplication(newApplication: Omit<TeacherPairApplication, 'applicationId'>) {
            this.loading = true
            try {
                // const created = await applicationApi.submitApplication(newApplication)
                // this.applications.unshift(created)
            } finally {
                this.loading = false
            }
        },

        /**
         * 处理申请
         * @param applicationId - 申请ID
         * @param status - 处理状态(1-已同意,2-已拒绝)
         */
        async processApplication(applicationId: number, status: number) {
            this.loading = true
            try {
                // await applicationApi.processApplication(applicationId, status)
                // const application = this.applications.find(a => a.applicationId === applicationId)
                // if (application) {
                //     application.status = status
                // }
            } finally {
                this.loading = false
            }
        },

        /**
         * 设置结对类型筛选
         * @param pairType - 结对类型(1-老带新帮扶,2-科技干部结对,3-新老师导师指导)
         */
        setPairTypeFilter(pairType: number | null) {
            this.filter.pairType = pairType
        },

        /**
         * 设置状态筛选
         * @param status - 状态(0-待处理,1-已同意,2-已拒绝)
         */
        setStatusFilter(status: number | null) {
            this.filter.status = status
        }
    },
    getters: {
        /**
         * 获取筛选后的申请
         */
        filteredApplications(state): TeacherPairApplication[] {
            let result = [...state.applications]

            if (state.filter.pairType !== null) {
                result = result.filter(a => a.pairType === state.filter.pairType)
            }

            if (state.filter.status !== null) {
                result = result.filter(a => a.status === state.filter.status)
            }

            return result
        },

        /**
         * 获取待处理的申请
         */
        pendingApplications(state): TeacherPairApplication[] {
            return state.applications.filter(a => a.status === 0)
        },

        /**
         * 获取申请统计
         */
        applicationStats(state) {
            return {
                total: state.applications.length,
                pending: state.applications.filter(a => a.status === 0).length,
                approved: state.applications.filter(a => a.status === 1).length,
                rejected: state.applications.filter(a => a.status === 2).length
            }
        }
    }
})
